package com.example.adapter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.example.adapter.ActiveAdapter.Info;
import com.example.juyixia.R;
import com.exanple.data.Activeinfo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class PendActiveAdapter extends BaseAdapter{
	private List<Activeinfo> data;
    private LayoutInflater layoutInflater;
    private Context context;
	 private SQLiteDatabase db;
	public PendActiveAdapter(Context context, List<Activeinfo> data) {
	    //传入的data，就是我们要在listview中显示的信息
	        this.context = context;
	        this.data = data;
	        this.layoutInflater = LayoutInflater.from(context); 
	    }
	    //这里定义了一个类，用来表示一个item里面包含的东西，像我的就是一个imageView和三个TextView，按自己需要来
	    public class Info {
	        public TextView activename;
	        public TextView parter;
	        public TextView activetime;
	        public TextView leader;
	        public TextView allip;
	        public TextView location;
	        public TextView currentname;
	        public Button sure;
	        public Button cancle;
	    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return data.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		 final Info info = new Info();
	        convertView = layoutInflater.inflate(R.layout.item_pendactive, null);
	        info.activename = (TextView) convertView
	                .findViewById(R.id.active_name);
	        info.parter = (TextView) convertView
	                .findViewById(R.id.active_parter);
       info.activetime= (TextView) convertView
	                .findViewById(R.id.active_time);
       info.leader=(TextView) convertView
               .findViewById(R.id.leader);
       info.allip=(TextView) convertView
               .findViewById(R.id.allip);
       info.location=(TextView) convertView
               .findViewById(R.id.location);
       info.currentname=(TextView) convertView
               .findViewById(R.id.currentname);
       info.sure=(Button) convertView
               .findViewById(R.id.sure);
       info.cancle=(Button) convertView
               .findViewById(R.id.cancle);

       info.cancle.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			info.sure.setEnabled(false);
			info.cancle.setEnabled(false);
		}
	});
	    info.sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				info.sure.setEnabled(false);
				info.cancle.setEnabled(false);
				new Thread() {
					 public void run() {
					 try {
					
					 Socket socket = null;
					
					 // 创建Socket
					 socket = new Socket(data.get(position).getLeaderip(), 22222); // IP一定是本地机器的ip
					 Map<String, String> replymap=new HashMap<String, String>();
					 replymap.put("用户名", data.get(position).getCurrentname());
					 replymap.put("坐标", data.get(position).getLocation());
					 replymap.put("活动名",data.get(position).getActivename() );
					 replymap.put("参与者", data.get(position).getParter());
					 replymap.put("allip", data.get(position).getAllip());
					 JSONObject replyjson=new JSONObject(replymap);
					 // 向服务器发送消息
					 PrintWriter out = new PrintWriter(new BufferedWriter(
					 new OutputStreamWriter(socket.getOutputStream(), "utf-8")), true);
					 out.println(replyjson.toString());
					
					 out.close(); 
				                	 BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
				 					String str = reader.readLine();
				 					JSONObject rjson=new JSONObject(str);
				 						Activeinfo.activelist.add(new Activeinfo(rjson.getString("活动名：") , rjson.getString("参与者："), "2017.6.9"));
				 					
					 // 关闭Socket
					 socket.close();
					   data.remove(position);
					 } catch (Exception e) {
					 // TODO: handle exception
					
					 }
					
					 handler.sendEmptyMessage(0);// 执行完毕后给handler发送一个空消息
					 }
					
					 // 定义Handler对象
					 private Handler handler = new Handler() {
					 @Override
					 // 当有消息发送出来的时候就执行Handler的这个方法
					 public void handleMessage(Message msg) {
					 super.handleMessage(msg);
					 // 处理UI
					
					 }
					 };
					 }.start();
			}
			
			
			
		});
	        info.activename.setText((String) data.get(position).getActivename());
	        info.parter.setText((String) data.get(position).getParter());
	        info.activetime.setText((String) data.get(position).getActivetime());
	       info.leader.setText((String) data.get(position).getLeader());
	       info.allip.setText((String) data.get(position).getAllip());
	       info.location.setText((String) data.get(position).getLocation());
	       info.currentname.setText((String) data.get(position).getCurrentname());
	        return convertView;
	}

}
